package com.masterclient.app.modules.search.contract;

import android.widget.Toast;

import com.eoner.managerlibrary.netsign.NetEnvManager;
import com.masterclient.app.httpservice.RetrofitUtil;
import com.masterclient.app.modules.search.SearchGoodBean;
import com.eoner.baselibrary.utils.Utils;
import com.google.gson.Gson;

import java.util.List;
import java.util.Map;

import io.reactivex.Observable;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

/**
 * 作者：sean
 * 创建时间：2017/6/25 11:44
 * 功能说明：
 */

public class GoodResultPresenter extends GoodResultContract.Presenter {
    @Override
    public void searchGoodList(Map<String,String> attributes, List<String> brand_ids, List<String> cids, String start_price, String end_price, String kw, String page, String page_size, String sort,String promotion_type) {

        Gson gson = new Gson();
        String attrs = gson.toJson(attributes);
        StringBuilder brands = new StringBuilder();
        if(brand_ids!=null){
            for(String brand:brand_ids){
                if(brands.length()==0){
                    brands.append(brand);
                }else {
                    brands.append(","+brand);
                }
            }
        }
        StringBuilder cidstr = new StringBuilder();
        if(cids!=null){
            for(String cid:cids){
                if(cidstr.length()==0){
                    cidstr.append(cid);
                }else {
                    cidstr.append(","+cid);
                }
            }
        }

        String sortOption = "";
        if("new".equals(sort)){
        }else {
            sortOption = sort;
        }

        Observable observable = RetrofitUtil.getInstance().getSearchGoodList(NetEnvManager.BASE_URL+"1/search/search",attrs,brands.toString(),cidstr.toString(),start_price,end_price,kw,page, page_size,sortOption,promotion_type);
        Disposable disposable = toSubscribe(observable, new Consumer<SearchGoodBean>() {
            @Override
            public void accept(@NonNull SearchGoodBean bean) throws Exception {
                if(bean!=null && bean.getData()!=null){
                    mView.showSearchGood(bean.getData());
                }else {
                    Toast.makeText(Utils.getContext(),bean.getMsg(),Toast.LENGTH_SHORT).show();
                    mView.loadFailed();
                }
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(@NonNull Throwable o) throws Exception {
                o.printStackTrace();
                mView.loadFailed();
            }
        });

        mRxManager.add(disposable);
    }
}
